package com.cee.config;

import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.Properties;
@Slf4j
@Configuration
@MapperScan(value = {"com.cee.dao","com.cee.sch.job.dao"})
public class DataSourceConfig  {
    @Value("${system.auditTrailRequired}")
    private boolean auditTrailRequired;

    @Bean
    public Interceptor paginationInterceptor() throws InterruptedException {
        log.info("Page helper Interceptor init...");
        // 分页插件
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("dialect", "mysql");
        p.setProperty("offsetAsPageNum", "false");
        p.setProperty("rowBoundsWithCount", "false");
        p.setProperty("pageSizeZero", "true");
        p.setProperty("reasonable", "false");
        p.setProperty("supportMethodsArguments", "true");
        p.setProperty("returnPageInfo", "none");
        pageHelper.setProperties(p);
        return pageHelper;
    }


    //MYBATIS-PLUS性能分析插件—PerformanceInterceptor
    @Bean
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        performanceInterceptor.setFormat(false);
        return performanceInterceptor;
    }

    @Bean
    public Interceptor DMLInterceptor() {
        MyBatisInterceptor dmlInterceptor = new MyBatisInterceptor(auditTrailRequired);
        return dmlInterceptor;
    }

    @Bean
    public PlatformTransactionManager transactionManager( DataSource dataSource) {

        return new CustomizedDataSourceTransactionManager(dataSource, auditTrailRequired);
    }
}
